import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // 管理员数据存放
    admin: {},
    // 用户管理数据
    userList: [],
    UserTotal: 100,
    // 反馈数据
    feedbackTableData: [],
    feedbackTotal: 100,
  },
  getters: {},
  mutations: {
    // 设置管理员信息
    setAdmin(state, admin) {
      state.admin = admin;
    },
    // 设置用户信息
    setUsers(state, userList) {
      state.userList = userList.records;
      state.UserTotal = userList.total;
    },
    // 设置反馈信息
    setFeedback(state, feedbackList) {
      state.feedbackTableData = feedbackList.records;
      state.feedbackTotal = feedbackList.total;
    },
  },
  actions: {
    // 获取管理员信息
    getAdmin(context) {
      axios({
        url: "/api/tnt/admin/refresh",
        method: "get",
        headers: {
          token: localStorage.getItem("token"),
        },
      }).then((resp) => {
        // console.log(resp.data);
        context.commit("setAdmin", resp.data.data);
      });
    },
    // 获取用户信息
    getUsers(context, params) {
      axios({
        url: "/api/tnt/users/page",
        method: "get",
        params,
        headers: {
          token: localStorage.getItem("token"),
        },
      }).then((resp) => {
        // console.log(resp.data.data);
        context.commit("setUsers", resp.data.data);
      });
    },
    // 获取反馈列表
    getFeedback(context, params) {
      axios({
        url: "/api/tnt/admin/feedbacks/page",
        method: "get",
        params,
        headers: {
          token: localStorage.getItem("token"),
        },
      }).then((resp) => {
        // console.log(resp.data.data);
        context.commit("setFeedback", resp.data.data);
      });
    },
  },
  modules: {},
});
